package com.zjcloud.jwgl.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zjcloud.common.annotation.Log;
import com.zjcloud.common.core.controller.BaseController;
import com.zjcloud.common.core.domain.AjaxResult;
import com.zjcloud.common.enums.BusinessType;
import com.zjcloud.jwgl.domain.AcScheduleConstraintTemplate;
import com.zjcloud.jwgl.domain.AcScheduleConstraint;
import com.zjcloud.jwgl.service.IAcScheduleConstraintTemplateService;
import com.zjcloud.common.utils.poi.ExcelUtil;
import com.zjcloud.common.core.page.TableDataInfo;

/**
 * 排课约束条件模板Controller
 * 
 * @author zjcloud
 * @date 2024-12-19
 */
@RestController
@RequestMapping("/jwgl/constraintTemplate")
public class AcScheduleConstraintTemplateController extends BaseController
{
    @Autowired
    private IAcScheduleConstraintTemplateService acScheduleConstraintTemplateService;

    /**
     * 查询排课约束条件模板列表
     */
    @PreAuthorize("@ss.hasPermi('jwgl:constraintTemplate:list')")
    @GetMapping("/list")
    public TableDataInfo list(AcScheduleConstraintTemplate acScheduleConstraintTemplate)
    {
        startPage();
        List<AcScheduleConstraintTemplate> list = acScheduleConstraintTemplateService.selectAcScheduleConstraintTemplateList(acScheduleConstraintTemplate);
        return getDataTable(list);
    }

    /**
     * 导出排课约束条件模板列表
     */
    @PreAuthorize("@ss.hasPermi('jwgl:constraintTemplate:export')")
    @Log(title = "排课约束条件模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AcScheduleConstraintTemplate acScheduleConstraintTemplate)
    {
        List<AcScheduleConstraintTemplate> list = acScheduleConstraintTemplateService.selectAcScheduleConstraintTemplateList(acScheduleConstraintTemplate);
        ExcelUtil<AcScheduleConstraintTemplate> util = new ExcelUtil<AcScheduleConstraintTemplate>(AcScheduleConstraintTemplate.class);
        util.exportExcel(response, list, "排课约束条件模板数据");
    }

    /**
     * 获取排课约束条件模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('jwgl:constraintTemplate:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(acScheduleConstraintTemplateService.selectAcScheduleConstraintTemplateById(id));
    }

    /**
     * 新增排课约束条件模板
     */
    @PreAuthorize("@ss.hasPermi('jwgl:constraintTemplate:add')")
    @Log(title = "排课约束条件模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AcScheduleConstraintTemplate acScheduleConstraintTemplate)
    {
        return toAjax(acScheduleConstraintTemplateService.insertAcScheduleConstraintTemplate(acScheduleConstraintTemplate));
    }

    /**
     * 修改排课约束条件模板
     */
    @PreAuthorize("@ss.hasPermi('jwgl:constraintTemplate:edit')")
    @Log(title = "排课约束条件模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AcScheduleConstraintTemplate acScheduleConstraintTemplate)
    {
        return toAjax(acScheduleConstraintTemplateService.updateAcScheduleConstraintTemplate(acScheduleConstraintTemplate));
    }

    /**
     * 删除排课约束条件模板
     */
    @PreAuthorize("@ss.hasPermi('jwgl:constraintTemplate:remove')")
    @Log(title = "排课约束条件模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(acScheduleConstraintTemplateService.deleteAcScheduleConstraintTemplateByIds(ids));
    }

    /**
     * 查询默认模板列表
     */
    @GetMapping("/default")
    public AjaxResult getDefaultTemplates()
    {
        List<AcScheduleConstraintTemplate> list = acScheduleConstraintTemplateService.selectDefaultTemplates();
        return success(list);
    }

    /**
     * 设置默认模板
     */
    @PreAuthorize("@ss.hasPermi('jwgl:constraintTemplate:edit')")
    @Log(title = "设置默认模板", businessType = BusinessType.UPDATE)
    @PutMapping("/setDefault/{id}")
    public AjaxResult setDefaultTemplate(@PathVariable Long id)
    {
        return toAjax(acScheduleConstraintTemplateService.setDefaultTemplate(id));
    }

    /**
     * 根据模板初始化约束条件
     */
    @PostMapping("/initConstraints")
    public AjaxResult initConstraints(@RequestBody AcScheduleConstraintTemplate template)
    {
        String templateName = template.getTemplateName();
        String semester = template.getRemark(); // 使用remark字段传递学期信息
        
        if (semester == null || semester.trim().isEmpty()) {
            return error("学期信息不能为空");
        }
        
        List<AcScheduleConstraint> constraints = acScheduleConstraintTemplateService.initConstraintsBySemester(templateName, semester);
        return success(constraints);
    }

    /**
     * 应用默认模板到指定学期
     */
    @PreAuthorize("@ss.hasPermi('jwgl:constraintTemplate:apply')")
    @Log(title = "应用默认模板", businessType = BusinessType.UPDATE)
    @PostMapping("/applyDefault/{semester}")
    public AjaxResult applyDefaultTemplate(@PathVariable String semester)
    {
        try {
            int result = acScheduleConstraintTemplateService.applyDefaultTemplate(semester);
            if (result > 0) {
                return success("成功应用默认模板，初始化了 " + result + " 个约束条件");
            } else {
                return error("应用默认模板失败，请检查是否存在默认模板");
            }
        } catch (Exception e) {
            logger.error("应用默认模板失败", e);
            return error("应用默认模板失败：" + e.getMessage());
        }
    }

    /**
     * 复制模板
     */
    @PreAuthorize("@ss.hasPermi('jwgl:constraintTemplate:add')")
    @Log(title = "复制模板", businessType = BusinessType.INSERT)
    @PostMapping("/copy/{sourceId}")
    public AjaxResult copyTemplate(@PathVariable Long sourceId, @RequestBody AcScheduleConstraintTemplate template)
    {
        String newTemplateName = template.getTemplateName();
        if (newTemplateName == null || newTemplateName.trim().isEmpty()) {
            return error("新模板名称不能为空");
        }
        
        // 检查模板名称是否已存在
        AcScheduleConstraintTemplate existingTemplate = acScheduleConstraintTemplateService.selectFirstByTemplateName(newTemplateName);
        if (existingTemplate != null) {
            return error("模板名称已存在，请使用其他名称");
        }
        
        return toAjax(acScheduleConstraintTemplateService.copyTemplate(sourceId, newTemplateName));
    }

    /**
     * 根据模板名称查询模板
     */
    @GetMapping("/name/{templateName}")
    public AjaxResult getByTemplateName(@PathVariable String templateName)
    {
        AcScheduleConstraintTemplate template = acScheduleConstraintTemplateService.selectFirstByTemplateName(templateName);
        return success(template);
    }
}